﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class DbTypeHelper
    {
        public DbTypeCheckResult CheckDbType(string data)
        {
            DbTypeCheckResult result = new DbTypeCheckResult();
            try
            {
                if (!string.IsNullOrEmpty(data))
                {
                    string _temp = data.ToLower();
                    if (_temp.Equals("int"))
                    {
                        result.TypeName = "int";
                        result.TypeLength = 0;
                        result.TypePrecision = 0;
                        result.TypeScale = 0;
                    }
                    else if (_temp.Equals("int(4)"))
                    {
                        result.TypeName = "int";
                        result.TypeLength = 4;
                        result.TypePrecision = 0;
                        result.TypeScale = 0;
                    }
                    else if (_temp.StartsWith("nvarchar(") && _temp.EndsWith(")"))
                    {
                        result.TypeName = "nvarchar";
                        result.TypeLength = int.Parse(_temp.Replace("nvarchar(", "").Replace(")", ""));
                        result.TypePrecision = 0;
                        result.TypeScale = 0;
                    }
                }
                else
                {
                    result.Result = 0;
                }
            }
            catch (Exception)
            {
                result.Result = -1;
                throw;
            }
            return result;
        }
    }

    public class DbTypeCheckResult
    {
        /// <summary>
        /// 结果:1：校验成功 0：传入参数为空  2：类型格式错误 3：未知的类型
        /// </summary>
        public int Result { get; set; }
        public string TypeName { get; set; }
        public int TypeLength { get; set; }
        public int TypePrecision { get; set; }
        public int TypeScale { get; set; }
    }
}
